const fs = require('fs')

// 获取view文件路径
function getFolder(path) {
  const components = []
  const files = fs.readdirSync(path)
  files.forEach((item) => {
    const stat = fs.lstatSync(`${path}/${item}`)
    if (stat.isDirectory() === true && item !== 'components') {
      components.push(`${path}/${item}`)
      components.push(...getFolder(`${path}/${item}`))
    }
  })
  return components
}



// 获取模板文件及路径
function getTemplateFile(data) {
  let { getType, newViewCommonent } = data
  switch (getType) {
    case 'new common component':
      return {
        path: 'src/components/{{properCase name}}/index.vue',
        templateFile: 'plop-template/component/common-components/index.hbs'
      }

    case 'new view':
      return {
        path: 'src/views/{{properCase name}}/index.vue',
        templateFile: 'plop-template/component/views/index.hbs'
      }

    case 'new view commonent':
      return {
        path: `${newViewCommonent}/components/{{properCase name}}.vue`,
        templateFile: 'plop-template/component/views-components/index.hbs'
      }

    case 'new API':
      return {
        path: `src/api/{{properCase name}}/index.js`,
        templateFile: 'plop-template/component/api/index.hbs'
      }
  }
}

module.exports = {
  description: 'create type',
  prompts: [
    {
      type: 'list',
      name: 'getType',
      message: '选择创建类型',
      choices: [
        'new common component',
        'new view',
        'new view commonent',
        'new API'
      ],
      default: 'new view'
    },

    {
      type: 'list',
      name: 'newViewCommonent',
      message: '请选择页面组件创建目录',
      choices: getFolder('src/views'),
      when: (answers) => {
        if (answers.getType == 'new view commonent') {
          return true
        }
      },
    },


    {
      type: 'checkbox',
      name: 'selectComponent',
      message: '请选择需要导入的公有组件路径',
      choices: getFolder('src/components'),
      when: (answers) => {
        if (answers.getType == 'new view commonent' || answers.getType == 'new view') {
          return true
        }
      }
    },

    {
      type: 'input',
      name: 'name',
      message: `请输入名称`,
      validate: (v) => {
        if (!v || v.trim === '')
          return `名称不能为空`
        else
          return true
      },
    },
  ],



  actions: (data) => {
    let actions = [
      {
        type: 'add',
        path: '',
        templateFile: ''
      },
      {
        type: 'append',
        path: `src/router/index.js`,
        pattern: /},/,
        templateFile: 'plop-template/component/views/router.hbs',
        data: {
          name: '{{name}}',
          menu: '{{menu}}'
        }
      }
    ]

    //  替换选择的公有组件路径及给予组件名称,便于模板识别
    console.log('data==>', data);
    if (data.selectComponent) {
      data.selectComponent = data.selectComponent.map(item => {
        return {
          path: item.replace('src', '@'),
          name: item.slice(item.lastIndexOf('/') + 1)
        }
      })
    }
    // 使用选择的路径替换创建的默认路径
    Object.assign(actions[0], getTemplateFile(data))

    // 根据是否选择创建路由判断是否在router.js 新建路由

    actions = data.getType == 'new view' ? actions : [actions[0]]

    return actions
  },
}

